package com.ychs.service;

import com.ychs.dao.MajorMapper;
import com.ychs.dao.MemberMapper;
import com.ychs.entity.Major;
import com.ychs.util.Constants;
import com.ychs.util.DBUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * copyright(c)2021 YYB.ALL rights Reserved
 * <p>
 * 描述:专业业务实现类
 *
 * @author 原玉波
 * @version 1.0
 * @date 2021/5/21
 */
public class MajorService implements IBaseService<Major> {
    private Logger logger = Logger.getLogger(this.getClass());
    /**
     * 添加记录
     *
     * @param major 记录对象
     * @return 是否成功
     */
    @Override
    public boolean add(Major major) {
        SqlSession session = DBUtil.getInstance().getSqlSession();
        MajorMapper mapper = session.getMapper(MajorMapper.class);
        int result = mapper.insert(major);
        session.commit();
        //session.close();
        DBUtil.close(session);
        return result == Constants.UPDATE_SUCCESS;
    }

    /**
     * 修改记录
     *
     * @param major 修改记录对象
     * @return 成功与否
     */
    @Override
    public boolean modify(Major major) {
        SqlSession session = DBUtil.getInstance().getSqlSession();
        MajorMapper mapper = session.getMapper(MajorMapper.class);
        int result = mapper.update(major);
        session.commit();
        //session.close();
        DBUtil.close(session);
        return result == Constants.UPDATE_SUCCESS;
    }

    /**
     * 删除记录
     *
     * @param id 待删除记录的主键
     * @return 是否成功
     */
    @Override
    public boolean remove(int id) {
        logger.info("enter remove service");
        SqlSession session = DBUtil.getInstance().getSqlSession();

        MemberMapper memberMapper = session.getMapper(MemberMapper.class);
        Map<String, Object> conditions = new HashMap<>();
        conditions.put("major", id);
        //查询专业在成员表中的引用情况
        int count = memberMapper.selectTotalNumber(conditions);
        logger.info("member：：：："+count);
        if (count != Constants.NOT_EXIST) {
            DBUtil.close(session);
            return false;
        }

        MajorMapper mapper = session.getMapper(MajorMapper.class);
        int result = mapper.delete(id);
        session.commit();
        //session.close();
        DBUtil.close(session);
        return result == Constants.UPDATE_SUCCESS;
    }

    /**
     * 查询单个对象
     *
     * @param id 查询对象的主键
     * @return 查询结果
     */
    @Override
    public Major searchById(int id) {
        SqlSession session = DBUtil.getInstance().getSqlSession();
        MajorMapper mapper = session.getMapper(MajorMapper.class);
        Major major = mapper.selectById(id);
        //session.close();
        DBUtil.close(session);
        return major;
    }

    /**
     * 查询多条记录
     *
     * @param conditions 查询条件
     * @return 查询结果
     */
    @Override
    public List<Major> search(Map<String, Object> conditions) {
        SqlSession session = DBUtil.getInstance().getSqlSession();
        MajorMapper mapper = session.getMapper(MajorMapper.class);
        List<Major> majors = mapper.select(conditions);
        //session.close();
        DBUtil.close(session);
        return majors;
    }

    public int searchTotalNumber(Map<String, Object> conditions) {
        SqlSession session = DBUtil.getInstance().getSqlSession();
        MajorMapper mapper = session.getMapper(MajorMapper.class);
        int result = mapper.selectTotalNumber(conditions);
        //session.close();
        DBUtil.close(session);
        return result;
    }
}
